#include <linMasterTask.h>
#include "timer_device.h"

#if LIN_MASTER_EN == 1

TaskState_t linmTaskState = TASK_STATE_INIT;
LIN_Device_Frame_t activelinmFrame;

uint8_t commandCount = 0;

typedef struct{
  uint8_t red;
  uint8_t green;
  uint8_t blue;
}RGB_t;

RGB_t rgbmTable[7] = {
  {255,0,0},
  {0,255,0},
  {0,0,255},
  {255,255,0},
  {0,255,255},
  {255,0,255},
  {255,255,255},
};
uint8_t index = 0;

LIN_Device_Frame_t linm_cmd_table[] = {
  [0] = {
      .frame_id = 0x11,
      .msg_type = LIN_MSG_TYPE_TX,
      .checksum = LIN_CHECKSUM_ENHANCED,
      .length   = LIN_BUFF_SIZE,
      .data[0]  = 0xF2,
  .data[1]  = 0xFF,
  .data[2]  = 0xFF,
  .data[3]  = 0xFF,
  .data[4]  = 0xFF,
  .data[5]  = 0xFF,
  .data[6]  = 0xFF,
  .data[7]  = 0xFF,
  },
  [1] = {
      .frame_id = 0x14,
      .msg_type = LIN_MSG_TYPE_RX,
      .checksum = LIN_CHECKSUM_ENHANCED,
      .length   = LIN_BUFF_SIZE,
  },
	[2] = {
		.frame_id = 0x15,
		.msg_type = LIN_MSG_TYPE_RX,
		.checksum = LIN_CHECKSUM_ENHANCED,
		.length   = LIN_BUFF_SIZE,
	},
	[3] = {
		.frame_id = 0x21,
		.msg_type = LIN_MSG_TYPE_RX,
		.checksum = LIN_CHECKSUM_ENHANCED,
		.length   = LIN_BUFF_SIZE,
	},
	[4] = {
		.frame_id = 0x34,
		.msg_type = LIN_MSG_TYPE_RX,
		.checksum = LIN_CHECKSUM_ENHANCED,
		.length   = LIN_BUFF_SIZE,
	},
	[5] = {
		.frame_id = 0x36,
		.msg_type = LIN_MSG_TYPE_RX,
		.checksum = LIN_CHECKSUM_ENHANCED,
		.length   = LIN_BUFF_SIZE,
	},
	[6] = {
		.frame_id = 0x35,
		.msg_type = LIN_MSG_TYPE_RX,
		.checksum = LIN_CHECKSUM_ENHANCED,
		.length   = LIN_BUFF_SIZE,
	},
};

void Timer0_Callback(void)//定时器中断	
{
  LINM_SendFrame(&linm_cmd_table[commandCount]);
  commandCount++;
  if (commandCount >= sizeof(linm_cmd_table)/sizeof(linm_cmd_table[0]))
  {
    commandCount = 0;
  }
}

void LIN_Master_ISR(LIN_Device_Frame_t *frame)
{
    activelinmFrame = *frame;
    TM_PostTask(TASK_ID_LINM);
}

void hanldeLinCommands(LIN_Device_Frame_t *frame)
{
    // Handle received command
    static uint8_t VerValid = 1;

   /*if((frame->frame_id == 0x22)|| \
      (frame->frame_id == 0x23)|| \
      (frame->frame_id == 0x24)|| \
      (frame->frame_id == 0x25)|| \
      (frame->frame_id == 0x26)|| \
      (frame->frame_id == 0x27))
   {
     if(frame->data[0] != 110)
     {
       VerValid = 0;
     }
   }
   if ((frame->frame_id == 0x34)||\
      (frame->frame_id == 0x36) ||\
      (frame->frame_id == 0x35) ||\
      (frame->frame_id == 0x15) ||\
      (frame->frame_id == 0x14) ||\
      (frame->frame_id == 0x21))
    {
        if(0 == VerValid)
        {
          CLM_SetRGBL(LED0,ROOM_TEMPERATURE, 0, 0, 0, 0, 0);
        }
        else if (linm_cmd_table[0].data[0] & 0x01)
        {
			CLM_SetRGBL(LED0,ROOM_TEMPERATURE, 255, 255, 255,255, 0);
        }
        else if (frame->data[0]&0x01)
        {
			CLM_SetRGBL(LED0,ROOM_TEMPERATURE, 255, 0, 0,255, 0);
        }
        else if (frame->data[0]&0x02)
        {
			CLM_SetRGBL(LED0,ROOM_TEMPERATURE, 0, 255, 0,255, 0);
        }
        else if (frame->data[0]&0x04)
        {
			CLM_SetRGBL(LED0,ROOM_TEMPERATURE,  0, 0,255,255, 0);
        }
        else
	{
			CLM_SetRGBL(LED0,ROOM_TEMPERATURE, 0, 0, 0, 0, 0);	
        }	
    }*/
}

void LINM_TaskHandler(void)
{
    switch(linmTaskState){
    case TASK_STATE_ACTIVE:
      hanldeLinCommands(&activelinmFrame);
      break;
    case TASK_STATE_INIT:
      LINM_Init();
      LINM_RegisterLINM_IRQ((lin_cb_func_t)LIN_Master_ISR);
      TIMER_Init(TIMER0,HW_TIMER_PERIODIC_MODE, 160000U, Timer0_Callback);/*10ms 定时器初始化*/
      linmTaskState = TASK_STATE_ACTIVE;
      break;
    }
}

#endif
